/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary.registration;

import components.utils.ComponentFactory;
import components.utils.CustomVerifier;
import components.TabPanel;
import control.Application;
import control.ControlClient;
import entity.Client;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import org.joda.time.DateTime;

/**
 *
 * @author Juan
 */
public class PEnterpriseRegistration extends TabPanel {

    /**
     * Creates new form Panel
     */
    public PEnterpriseRegistration() {
        initComponents();
        
        ComponentFactory.attachVerifier("Por favor especifique el nombre del cliente.", TFName);
        ComponentFactory.attachVerifier("Por favor especifique el número de documento del cliente.", TFIdNum);
        ComponentFactory.attachVerifier("Por favor especifique el nombre del contacto en la empresa.", TFContact);
        ComponentFactory.attachVerifier("Por favor especifique el tipo de identificación.", TFOtherId);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BGIdType = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        TFName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        TFIdNum = new javax.swing.JTextField();
        TFOtherId = new javax.swing.JTextField();
        pPhoneNumbers = new components.panels.PPhoneNumbers();
        pAddresses = new components.panels.PAddresses();
        jLabel3 = new javax.swing.JLabel();
        TFCity = new javax.swing.JTextField();
        BRegisterClient = new javax.swing.JButton();
        BCancel = new javax.swing.JButton();
        pEmails = new components.panels.PEmails();
        jLabel4 = new javax.swing.JLabel();
        TFContact = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        TFContactPosition = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TABillingDates = new javax.swing.JTextArea();
        jLabel7 = new javax.swing.JLabel();
        TFBillingDeliveryMethod = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        TABillingComments = new javax.swing.JTextArea();
        RBNIT = new javax.swing.JRadioButton();
        RBOther = new javax.swing.JRadioButton();

        jLabel1.setText("Nombre *:");

        jLabel2.setText("Identificación *:");

        TFOtherId.setEnabled(false);

        jLabel3.setText("Ciudad:");

        BRegisterClient.setText("Registrar Cliente");
        BRegisterClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BRegisterClientActionPerformed(evt);
            }
        });

        BCancel.setText("Cancelar Registro");
        BCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelActionPerformed(evt);
            }
        });

        jLabel4.setText("Persona Contacto *:");

        jLabel5.setText("Cargo del Contacto:");

        jLabel6.setText("Fechas y Horarios de Radicación de Facturas:");

        TABillingDates.setColumns(20);
        TABillingDates.setRows(5);
        jScrollPane1.setViewportView(TABillingDates);

        jLabel7.setText("Forma de envío de facturación:");

        jLabel8.setText("Observaciones:");

        TABillingComments.setColumns(20);
        TABillingComments.setRows(5);
        jScrollPane2.setViewportView(TABillingComments);

        BGIdType.add(RBNIT);
        RBNIT.setSelected(true);
        RBNIT.setText("NIT");

        RBOther.setText("Otro:");
        RBOther.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RBOtherActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pPhoneNumbers, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pEmails, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pAddresses, javax.swing.GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(TFContactPosition)
                            .addComponent(TFName)
                            .addComponent(TFCity)
                            .addComponent(TFContact)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(RBNIT)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(RBOther))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(TFBillingDeliveryMethod, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                            .addComponent(BRegisterClient)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(BCancel))
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)))
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(TFOtherId, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6)
                                .addComponent(TFIdNum, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFIdNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TFOtherId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(RBNIT)
                    .addComponent(RBOther))
                .addGap(9, 9, 9)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFContact, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFContactPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pPhoneNumbers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pEmails, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pAddresses, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFBillingDeliveryMethod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BRegisterClient)
                    .addComponent(BCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void BCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelActionPerformed
        buttonTabComponent.closeTab();
    }//GEN-LAST:event_BCancelActionPerformed

    private void BRegisterClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BRegisterClientActionPerformed
        //New client entity creation.
        Client client = new Client();
        client.setType(Client.ENTERPRISE);
        
        //Custom verifier instance used to validate the fields.
        CustomVerifier currentVerifier;
        
        //Name initial validation.
        currentVerifier = (CustomVerifier) TFName.getInputVerifier();
        if (currentVerifier.isValid()) {
            client.setName(TFName.getText().trim());
        } else {
            return;
        }
        
        //ID number initial validation.
        currentVerifier = (CustomVerifier) TFIdNum.getInputVerifier();
        if (currentVerifier.isValid()) {
            client.setIdNum(TFIdNum.getText().trim());
        } else {
            return;
        }
        
        //Setting city.
        client.setCity(TFCity.getText());
        
        //Contact initial validation.
        currentVerifier = (CustomVerifier) TFContact.getInputVerifier();
        if (currentVerifier.isValid()) {
            client.setContact(TFContact.getText().trim());
        } else {
            return;
        }
        
        //Setting contact position.
        client.setContactPosition(TFContactPosition.getText());
        
        //ID Type initial validation.
        currentVerifier = (CustomVerifier) TFOtherId.getInputVerifier();
        if (RBOther.isSelected()) {
            if (currentVerifier.isValid()) {
                client.setIdType(TFOtherId.getText().trim());
            } else {
                return;
            }
        } else {
            client.setIdType(Client.NIT);
        }
        
        //Addresses, Emails and Phone Numbers initial validations.
        ArrayList addresses = pAddresses.getAddresses();
        ArrayList emails = pEmails.getEmails();
        ArrayList phoneNumbers = pPhoneNumbers.getPhoneNumbers();
        
        if (phoneNumbers.isEmpty()) {
            JOptionPane.showMessageDialog(this.getTopLevelAncestor(),
                    "Por favor especifique al menos un número de teléfono.", "Advertencia", JOptionPane.WARNING_MESSAGE);
            return;
        } else {
            if (!addresses.isEmpty()) {
                client.setAddresses(addresses);
            }
            if (!emails.isEmpty()) {
                client.setEmails(emails);
            }
            client.setPhoneNumbers(phoneNumbers);
        }
        
        //Setting billing conditions.
        client.setBillingComments(TABillingComments.getText());
        client.setBillingDates(TABillingDates.getText());
        client.setBillingDeliveryMethod(TFBillingDeliveryMethod.getText());
        
        //Setting current date
        client.setRegistrationDate(new DateTime());
        
        //Placeholder for actual database persitence:
        client.setAbbreviation((String) new ControlClient().assingAbbreviation(client.getName()));
        Application.CLIENT_MANAGER.addObject(client);
        JOptionPane.showMessageDialog(this.getTopLevelAncestor(),
                "El cliente se ha registrado con éxito.",
                "Información", JOptionPane.INFORMATION_MESSAGE);
        buttonTabComponent.closeTab();
    }//GEN-LAST:event_BRegisterClientActionPerformed

    private void RBOtherActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RBOtherActionPerformed
        TFOtherId.setEditable(RBOther.isSelected());
        System.out.println("yay");
    }//GEN-LAST:event_RBOtherActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BCancel;
    private javax.swing.ButtonGroup BGIdType;
    private javax.swing.JButton BRegisterClient;
    private javax.swing.JRadioButton RBNIT;
    private javax.swing.JRadioButton RBOther;
    private javax.swing.JTextArea TABillingComments;
    private javax.swing.JTextArea TABillingDates;
    private javax.swing.JTextField TFBillingDeliveryMethod;
    private javax.swing.JTextField TFCity;
    private javax.swing.JTextField TFContact;
    private javax.swing.JTextField TFContactPosition;
    private javax.swing.JTextField TFIdNum;
    private javax.swing.JTextField TFName;
    private javax.swing.JTextField TFOtherId;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private components.panels.PAddresses pAddresses;
    private components.panels.PEmails pEmails;
    private components.panels.PPhoneNumbers pPhoneNumbers;
    // End of variables declaration//GEN-END:variables
}
